﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using EventLogger.DataLayer;

namespace EventLogger.Web
{
    // NOTE: If you change the class name "Service" here, you must also update the reference to "Service" in Web.config.
    public class LoggerService : ILoggerService
    {        
        public void RaiseEvent(string eventName, string machineName, string userName, string eventData)
        {
            using (var context=new EventLoggerEntities())
            {
                var eventDefinition = context.EventDefinitions.Where(e => e.Name == eventName).FirstOrDefault();
                if (eventDefinition == null)
                {
                    throw new Exception("Event not found");
                }

                var entity = new EventEntity
                {
                    Event = eventDefinition,
                    EventData = eventData,
                    MachineName = machineName,
                    UserName = userName,
                    TimeRaised = DateTime.Now
                };

                context.AddToEvents(entity);
                context.SaveChanges();
            }
        }       
    }
}
